package com.ymatou.iapi.trading.sellerquery2.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.Order;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.iapi.trading.operate.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.trading.operate.service.PlaceOrderCall;
import com.ymatou.iapi.trading.ordersync.service.SyncVerifyService;
import com.ymatou.iapi.trading.sellerquery2.parameter.GetOrderProductsBean;
import com.ymatou.iapi.trading.sellerquery2.parameter.resp.GetOrderProductsResp;
import com.ymatou.iapi.trading.sellerquery2.parameter.resp.OrderProductInfoLite;
import com.ymatou.iapi.trading.sellerquery2.service.GetOrderProductsCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

/**
 * @date 2017-04-13
 * @desc 根据商品名称获取商品信息
 */
@TestSuite
public class Ts_GetOrderProducts {

	private static GetOrderProductsBean request;
	private static GetOrderProductsCall call;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据商品名称获取商品信息");
	}

	@Before
	public void caseUp() {
		request = new GetOrderProductsBean();
		call = new GetOrderProductsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 * 
	 * 
	 * 一次下单订单下面5个商品
	 *
	 */
	private static PlaceOrderResp placeOrder() {

		PlaceOrderResp orderResp = new PlaceOrderResp();

		PlaceOrderReq placeorderBean = PlaceOrderService.getDefaultOrderReq();

		placeorderBean
				.getOrderGroups()
				.get(0)
				.getOrders()
				.get(0)
				.setOrderItems(
						Arrays.asList(
								PlaceOrderService.getDefaultOrderItemVo(),
								PlaceOrderService.getDefaultOrderItemVo(),
								PlaceOrderService.getDefaultOrderItemVo(),
								PlaceOrderService.getDefaultOrderItemVo(),
								PlaceOrderService.getDefaultOrderItemVo()));

		int userId = placeorderBean.getUserId();

		int sellerId = placeorderBean.getOrderGroups().get(0).getSellerId();

		PlaceOrderCall placeorderCall = new PlaceOrderCall();

		try {

			placeorderCall.setData(false, placeorderBean);

			placeorderCall.callService();

			JSONArray orderArray = placeorderCall.getOrderIds();

			List<Order> orderList = new ArrayList<Order>();

			for (int i = 0; i < orderArray.length(); i++) {

				Order order = new Order();

				order.setOrderId(orderArray.getInt(i));
				order.setSellerId(sellerId);

				orderList.add(order);
			}

			int tradingId = placeorderCall.getTradingId();
			// 根据交易号计算需要支付的金额
			BigDecimal payAmount = new OrderCallService()
					.getNeedPrePayAmountOfCashBymainOrderId(tradingId);

			orderResp.setTradingId(tradingId);
			orderResp.setOrderList(orderList);
			orderResp.setPayAmount(payAmount);
			orderResp.setUserId(userId);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return orderResp;

	}
	private void verify() {

		GetOrderProductsResp getOrderProductsResp = call.getOrderProductsResp();

		List<OrderProductInfoLite> orderProductInfoLites = getOrderProductsResp
				.getProductInfoList();

		if (orderProductInfoLites.size() > 1) {

			Logger.verifyEquals(true, orderProductInfoLites.size() <= 1000,
					"校验接口返回商品总记录数不大于1000");
			int i = 0;
			for (OrderProductInfoLite orderProductInfoLite : orderProductInfoLites) {

				if (i > 20) {

					break;
				}

				String proId = orderProductInfoLite.getProductId();
				String proName = orderProductInfoLite.getProductTile();

				List<Map> maps = new DefaultWapper()
						.executeSql("select i.sTitle from ymt_orderinfo i join ymt_orders  o on i.iOrderId=o.iOrderId where o.iBuyerId="
								+ request.getSellerId()
								+ " and i.sProductId='"
								+ proId + "'");

				Logger.verifyEquals(proName,
						(String) maps.get(0).get("sTitle"), "校验接口返回商品title");

				i++;
			}

		}

	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderProducts_001() {
		Logger.start(true, "根据商品名称获取商品信息");
		try {

			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));

			request.setSellerId(sellerId);
			request.setProductName("自动化商品");

			request.setTradingStatus(2);

			request.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-10,
					YMTDateUtil.YMDTHMS_STRING));
			request.setEndTime(YMTDateUtil.getBeforeOrNextDay(2,
					YMTDateUtil.YMDTHMS_STRING));

			call.setData(request);
			call.callService();
			Logger.verifyEquals("200", call.getCode(), "验证code");

			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderProducts_002() {
		Logger.start(true, "开始时间为空");
		try {

			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));

			request.setSellerId(sellerId);
			request.setProductName("自动化");

			request.setTradingStatus(2);

			/*			request.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-10,
								YMTDateUtil.YMDTHMS_STRING));*/
			request.setEndTime(YMTDateUtil.getBeforeOrNextDay(2,
					YMTDateUtil.YMDTHMS_STRING));

			call.setData(request);
			call.callService();
			Logger.verifyEquals("200", call.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderProducts_003() {
		Logger.start(true, "截止时间为空");
		try {

			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));

			request.setSellerId(sellerId);
			request.setProductName("自动化");

			request.setTradingStatus(2);

			request.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-10,
					YMTDateUtil.YMDTHMS_STRING));
			/*			request.setEndTime(YMTDateUtil.getBeforeOrNextDay(2,
								YMTDateUtil.YMDTHMS_STRING));*/

			call.setData(request);
			call.callService();
			Logger.verifyEquals("200", call.getCode(), "验证code");

			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderProducts_004() {
		Logger.start(true, "截止时间为空");
		try {

			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));

			request.setSellerId(sellerId);
			request.setProductName("自动化");

			request.setTradingStatus(2);

			request.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-10,
					YMTDateUtil.YMDTHMS_STRING));
			/*			request.setEndTime(YMTDateUtil.getBeforeOrNextDay(2,
								YMTDateUtil.YMDTHMS_STRING));*/

			call.setData(request);
			call.callService();
			Logger.verifyEquals("200", call.getCode(), "验证code");
			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderProducts_005() {
		Logger.start(true, "订单状态为空");
		try {

			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));

			request.setSellerId(sellerId);
			request.setProductName("自动化");

			//request.setTradingStatus(2);

			request.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-10,
					YMTDateUtil.YMDTHMS_STRING));
			/*			request.setEndTime(YMTDateUtil.getBeforeOrNextDay(2,
								YMTDateUtil.YMDTHMS_STRING));*/

			call.setData(request);
			call.callService();
			Logger.verifyEquals("400", call.getCode(), "验证code");
			Logger.verifyEquals("100", call.getBCode(), "验证bcode");
			Logger.verifyEquals("tradingStatus:订单状态不能为0", call.getMsg(),
					"验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderProducts_006() {
		Logger.start(true, "一个订单下有超过3个商品,根据商品名称获取商品信息");
		try {

			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));

			int orderId = placeOrder().getOrderList().get(0).getOrderId();

			SyncVerifyService.getSyncResult(orderId);

			request.setSellerId(sellerId);
			request.setProductName("自动化商品");

			request.setTradingStatus(1);

			request.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-1,
					YMTDateUtil.YMDTHMS_STRING));
			request.setEndTime(YMTDateUtil.getBeforeOrNextDay(1,
					YMTDateUtil.YMDTHMS_STRING));

			call.setData(request);
			call.callService();
			Logger.verifyEquals("200", call.getCode(), "验证code");

			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}